package com.wkq.move.module.main.frame.view

import android.content.Context
import android.graphics.Color
import android.widget.LinearLayout
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.lib_base.utlis.StatusBarUtil
import com.wkq.move.R
import com.wkq.move.module.main.ui.adapter.ClassificationFragmentPagerAdapter
import com.wkq.move.module.main.ui.fragment.ClassificationFragment
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 *
 * 作者:吴奎庆
 *
 * 时间:2020/5/17
 *
 * 用途:
 */


class ClassificationView : MvpView {

    var mFragment: ClassificationFragment
    var myAdapter: ClassificationFragmentPagerAdapter? = null

    var titleList = listOf<String>("犯罪/历史", "魔幻/科幻", "惊悚/灵异", "都市/情感", "选秀/综艺", "动漫/卡通")

    constructor(mFragment: ClassificationFragment) {
        this.mFragment = mFragment
    }

    fun initView() {

        StatusBarUtil.setDarkMode(mFragment.activity)
        myAdapter = ClassificationFragmentPagerAdapter(
            mFragment.activity as Context,
            mFragment.childFragmentManager
        )

        mFragment.binding.vpInformation.adapter = myAdapter

//        mFragment.binding.vpInformation.currentItem = 0
//        mFragment.binding.vpInformation.offscreenPageLimit = 3


        val magicIndicator = mFragment.binding.magicIndicator
        val commonNavigator = CommonNavigator(mFragment.activity)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return if (titleList == null) 0 else titleList.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView: SimplePagerTitleView =
                    ColorTransitionPagerTitleView(context)
                simplePagerTitleView.setText(titleList.get(index))
                simplePagerTitleView.normalColor = Color.parseColor("#88ffffff")
                simplePagerTitleView.selectedColor = Color.WHITE
                simplePagerTitleView.setOnClickListener {
                    mFragment.binding.vpInformation.currentItem = index
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator? {
                val indicator = LinePagerIndicator(context)
                indicator.setColors(Color.parseColor("#40c4ff"))
                return null
            }
        }
        magicIndicator.navigator = commonNavigator
        val titleContainer =
            commonNavigator.titleContainer // must after setNavigator

        titleContainer.showDividers = LinearLayout.SHOW_DIVIDER_MIDDLE
        titleContainer.dividerPadding = UIUtil.dip2px(mFragment.activity, 15.0)
        titleContainer.dividerDrawable = mFragment.resources.getDrawable(R.drawable.simple_splitter)

        magicIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(magicIndicator, mFragment.binding.vpInformation)


    }

}